package edu.wpi.cs509.representation;

import java.util.logging.Logger;

/**
 * ActionMove is a specific type of action (a move) whereby a piece is moved
 * from one on-board location to another on-board location.
 * @author cwinsor
 */
public class ActionMove extends ActionBaseClass {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor
	 * @param name - gives this action a name
	 */
	public ActionMove(String name, Gameboard gameboard,
			PlayerPiece pce, Location loc) {

		super(name, gameboard, pce, loc);
	}
	
	/**
	 * doCheck - check the move for legality
	 */
	protected void doStrategoRulesCheck() throws ActionNotLegalException {

		// debug message
		Logger.getLogger("global").info("here"); 

		// check the destination location is not the boneyard
		if (isBoneyardDestination()) {
			setErrMessage("the destination was the boneyard (cannot use ActionMove)");
			throw new ActionNotLegalException();
		}	

		// check the destination is occupiable (not the lake) 
		if (!isOccupiableDestination()) {
			setErrMessage("destination is not occupiable");
			throw new ActionNotLegalException();	
		}

		// check the destination is not the same as the current location
		if (destinationIsTheSameAsCurrentLocation()){
			setErrMessage("the destination was the same as the current location");
			throw new ActionNotLegalException();
		}

		// check the movement is strictly x or y (not diagonal)
		if (!movementIsStrictlyYorX()){
			setErrMessage("the move changes both x and y position (diagonal)");
			throw new ActionNotLegalException();	
		}

		// check the piece can move that number of spaces
		if (!partCanMoveThatNumberOfSpaces()){
			if (pieceBeingMoved.maxSpacesCanMove() == 0) 
				setErrMessage("the part can not be moved");
			else if (pieceBeingMoved.maxSpacesCanMove() == 1) 
				setErrMessage("the part can only move 1 square");
			else
				setErrMessage("the part can only move move " +
						pieceBeingMoved.maxSpacesCanMove() + " squares");
			throw new ActionNotLegalException();	
		}

		// check if a multi-square-move with interveneing pieces
		if (multiSquareMovePiecesAlongTheWay()) {
			setErrMessage("there are pieces along the way - cannot jump over pieces");
			throw new ActionNotLegalException();	
		}

		// check if a multi-square-move with intervening swamp
		if (multiSquareMoveSwampAlongTheWay()) {
			setErrMessage("there is swamp along the way - need to go around swamp");
			throw new ActionNotLegalException();	
		}

		// check if a multi-square-move ending in an attack
		if (multiSquareAttack()) {
			setErrMessage("the scout cannot attack from a distance");
			throw new ActionNotLegalException();	
		}


		// if this an attack, but the piece can't attack
		if (!isAnAttack() && !canAttack()) {
			setErrMessage("the piece cannot attack");
			throw new ActionNotLegalException();	
		}
	}


	/**
	 * doExecute - perform the action
	 * will throw an exception if the move is illegal	
	 */
	protected void doExecute() throws ActionNotLegalException, ActionEndOfGameException {

		// debug message
		Logger.getLogger("global").info("here");
	
		// execute the move (generic)
		super.doExecute();

		return;
	}


}
